import cv2
import numpy as np
import matplotlib.pyplot as plt

# 图像加法
# 两个图像应该具有相同大小和类型，或者第二个图像可以是标量值
x = np.uint8([250])
y = np.uint8([10])  # unsigned int-8

print(cv2.add(x, y))  # 250 + 10 = 260 -> 255
print(x + y)  # 250 + 10 = 260 % 256 = 4

# 实例
# 导入图像
img1 = cv2.imread("test.jpg")
assert isinstance(img1, np.ndarray)

img2 = cv2.imread("test1.jpg")
assert isinstance(img2, np.ndarray)

# 调整图像尺寸，确保两个图像具有相同的尺寸和通道数
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

# 对图像进行相加
img3 = cv2.add(img1, img2)
plt.imshow(img3[:, :, ::-1])
plt.show()
img4 = img1 + img2
plt.imshow(img4[:, :, ::-1])
plt.show()

# 执行图像减法操作
subtracted_image = cv2.subtract(img1, img2)
# 显示结果图像
cv2.imshow("Subtracted Image", subtracted_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
